#!/bin/bash

GPIO_CAM_PWR_EN_2V8=324
GPIO_CAM_PWR_EN_12V=325

SYSFS_GPIO=/sys/class/gpio
SYSFS_CAM_PWR_EN_2V8=${SYSFS_GPIO}/PBB.00
SYSFS_CAM_PWR_EN_12V=${SYSFS_GPIO}/PBB.01

function gpio_init()
{
	if [[ ! -d ${SYSFS_CAM_PWR_EN_2V8} ]]; then
		echo ${GPIO_CAM_PWR_EN_2V8} > ${SYSFS_GPIO}/export
	fi

	if [[ ! -d ${SYSFS_CAM_PWR_EN_12V} ]]; then
		echo ${GPIO_CAM_PWR_EN_12V} > ${SYSFS_GPIO}/export
	fi
}

function cam_pwr_operate()
{
	case $1 in
		"on" | "ON")
			echo 1 > ${SYSFS_CAM_PWR_EN_2V8}/value
			echo 1 > ${SYSFS_CAM_PWR_EN_12V}/value
			echo -e "[INFO] Camera Power is ON."
			;;
		"off" | "OFF")
			echo 0 > ${SYSFS_CAM_PWR_EN_2V8}/value
			echo 0 > ${SYSFS_CAM_PWR_EN_12V}/value
			echo -e "[INFO] Camera Power is OFF."
			;;
		*)
			echo -e "[ERROR] Unknown power operation: $1, do nothing!"
			;;
	esac
}

if [[ $(id -u) -ne 0 ]]; then
	echo -e "[ERROR] Please run as root!"
	exit 1
fi

gpio_init

cam_pwr_operate off
sleep 6
cam_pwr_operate on
sleep 2
echo -e "[INFO] Camera Power RESET is Done."

./gmsl_serdes_setup-SYIMX390.sh
echo -e "[INFO] SerDes setting up is Done."
